<?php
/**
 * Created by PhpStorm.
 * User: sofwu
 * Date: 2019/1/11
 * Time: 下午3:13
 */
namespace App\Http\Controllers\Api\V1;


use App\Http\Transformer\OrderTransformer;
use App\Model\Goods;
use App\Model\Order;
use App\Model\OrderGoods;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;

class OrderController extends Controller
{
    public function __construct()
    {
        $this->middleware('api.auth', ['only' => ['store', 'addNice']]);
    }

    /**
     * 储存订单
     * @return \Dingo\Api\Http\Response
     */
    public function store()
   {
       $rules = [
           'goods_id' => ['required'],
           'total_amount' => ['required'],
           'name' => ['required'],
           'user_note' => ['required'],
           'is_public' => ['required'],
       ];

       $payload = app('request')->all();

       $validator = app('validator')->make($payload, $rules);

       if ($validator->fails()) {
           throw new \Dingo\Api\Exception\ResourceException('Could not post order store.', $validator->errors());
       }

       try{
           $user = $this->auth->user();
           $goods = Goods::find($payload['goods_id']);
           $order = new Order;
           $order->order_sn = $order->get_order_sn();
           $order->user_id = $user->id;
           $order->total_amount = $payload['total_amount'];
           $order->order_status = 0;
           $order->pay_status = 0;
           $order->is_public = $payload['is_public'];
           $order->name = $payload['name'];
           $order->user_note = $payload['user_note'];
           $order->save();

           $orderGoods = new OrderGoods;
           $orderGoods->goods_id = $goods->id;
           $orderGoods->goods_name = $goods->name;
           $order->orderGoods()->save($orderGoods);

           return $this->response->item($order, new OrderTransformer);

       }catch (\Exception $exception){
           Log::info($exception->getMessage());
           throw new \Dingo\Api\Exception\StoreResourceFailedException($exception->getMessage());
       }
   }


    /**
     * 查看订单
     * @return \Dingo\Api\Http\Response
     */
    public function orderList(){
        $orders = Order::where(['order_status'=>1, 'pay_status'=>1, 'is_public'=>1])->orderBy('id', 'desc')->paginate();
        return $this->response->paginator($orders, new OrderTransformer);
    }


    /**
     * 订单点赞
     * @return mixed
     */
    public function addNice(){
        $rules = [
            'order_sn' => ['required'],
        ];

        $payload = app('request')->only('order_sn');

        $validator = app('validator')->make($payload, $rules);

        if ($validator->fails()) {
            throw new \Dingo\Api\Exception\ResourceException('Could not post order addNice.', $validator->errors());
        }

        $order = Order::where(['order_sn'=> $payload['order_sn'], 'order_status'=>1, 'pay_status'=>1])->first();
        if(!$order){
            throw new \Dingo\Api\Exception\ResourceException('记录不存在');
        }


        $user = $this->auth->user();
        $nice = Redis::sismember('order:'.$order->id, $user->id); //查询用户是否已经点赞过该订单
        if(!$nice){
            $result = Redis::sadd('order:'.$order->id, $user->id);
            if(!$result){
                throw new \Dingo\Api\Exception\ResourceException('你已经点赞过了');
            }

            $niceCount = Redis::scard('order:'.$order->id);
            return $this->response->array(['data'=> ['nice_count'=>$niceCount]]);
        }else{
            throw new \Dingo\Api\Exception\ResourceException('你已经点赞过了');
        }


    }

    /**
     * 查看订单详情
     * @return \Dingo\Api\Http\Response
     */
    public function orderInfo(){
        $rules = [
            'order_sn' => ['required'],
        ];

        $payload = app('request')->only('order_sn');

        $validator = app('validator')->make($payload, $rules);

        if ($validator->fails()) {
            throw new \Dingo\Api\Exception\ResourceException('Could not post order addNice.', $validator->errors());
        }

        $order = Order::where(['order_status'=>1, 'pay_status'=>1, 'is_public'=>1, 'order_sn'=>$payload['order_sn']])->first();
        return $this->response->item($order, new OrderTransformer);
    }
}